<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/19
 * Time: 16:51

 * 订单类
 */

namespace app\console\controller;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Build;
use think\Controller;
use think\Db;
use think\Request;
use think\Url;
use app\interfaces\controller\Alipay;
use think\Exception;
use think\Session;
use app\console\model\RefundOrderAudit;
use think\Cache;
class Order extends Controller
{
    /**
     * @return array|mixed|\think\response\View
     * 订单列表
     */
    public function index()
    {

        $param = Request::instance()->param();
        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10

        $where = [];
        $auding_status = Session::get('auding_status');
        if($auding_status == 1){
            $where['shop_id'] = ['eq',\session('shop_id')];
        }
        $where['o.order_type'] = ['eq', 1];

        if (!empty($param['is_certification'])) $where['m.is_certification'] = ['eq', $param['is_certification']];
        if (!empty($param['status'])){
            if($param['status'] == 2 || $param['status'] == 6){
                $where['o.status'] = ['in', $this->status];
            }else{
                $where['o.status'] = ['eq', $param['status']];
            }
        }
        if (!empty($param['shop_id'])) $where['shop_id'] = ['eq', $param['shop_id']];
        if (!empty($param['title'])) $where['m.username|o.shop_order_number|m.car_number'] = ['like', '%'.$param['title'].'%'];
        if (!empty($param['interval_timer'])) {
            $interval_timer = explode('/', $param['interval_timer']);
            $where['o.create_time'] = ['between', [$interval_timer[0], $interval_timer[1]]];
        }
        $data = \app\console\model\Order::alias('o')
            ->join('member m', 'o.member_id = m.id','left')
            ->join('shop s', 'o.shop_id = s.id','left')
            ->field('m.username,o.*,m.car_number,m.is_certification,s.shop_title')
            ->where($where)
            ->fetchSql(false)
            ->order('id','desc')
            ->paginate(10, false, ['query' =>$param])->each(function($item,$key) use($level){
                if(session('role_id') != 1){ //非超级管理员
                    $item->username = hideStr($item->username,1,6);
                }

                if($item->is_certification == 2 && !empty($item->car_number)){
                    //$item->car_number = '('.$item->car_number.')';
                }

                if($item['status'] == 12 || $item['status'] == 10 || $item['status'] == 11){
                    $item->refund_create_time = Db::name('order_refund')->where('order_id',$item['id'])->value('create_time');
                }
                $item->refund_button = false;
                $count = (new RefundOrderAudit())->where('order_id',$item->id)->where('status','11')->count();
                if($level == 5){
                    if($count == 0){
                        $item->refund_button = true;
                    }
                }
                if($level == 7){
                    if($count == 1){
                        $item->refund_button = true;
                    }
                }
                if($level == 8){
                    if($count == 2){
                        $item->refund_button = true;
                    }
                }
                if($level == 9){
                    if($count == 3){
                        $item->refund_button = true;
                    }
                }
                if($level == 10){
                    if($count == 4){
                        $item->refund_button = true;
                    }
                }
            });

        $total_count = Db::name('order o')
            ->join('member m', 'o.member_id = m.id')
            ->join('shop s', 'o.shop_id = s.id')
            ->field('o.*,m.username,s.shop_title')
            ->where($where)->count();
        $total_price = Db::name('order o')
            ->join('member m', 'o.member_id = m.id')
            ->join('shop s', 'o.shop_id = s.id')
            ->field('o.*,m.username,s.shop_title')
            ->where($where)->sum('amount');
        $shop_list = Db::name('shop s')->field('id,shop_title')->select();

        $service_people_price = Db::name('shop_service_price')
            ->alias('ssp')
            ->join('single s','ssp.single_id = s.id','left')

            ->where('s.parent_id','gt',0)
            ->field('s.id,s.cat_name,ssp.person_price')
            ->select();
        $template = '';
        if(isset($param['table'])) $template = 'service_order_table';
        return view($template, ['data' => $data,
            'service_people_price' => $service_people_price,
            'shop_list' => $shop_list,
            'total_count' => $total_count,
            'total_price' => $total_price,
        ]);
    }


    public function excel()
    {

    }

    /**
     * @param $id
     * 订单修改
     */
    public function update($id)
    {

        $param = Request::instance()->param();
        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
        $find = Db::name('order')->find($id);

        $auding_status = Session::get('auding_status');
        if($auding_status == 2){
            Db::name('order o')->where('id',$id)->setField('is_read',1);
        }else{
            Db::name('order o')->where('id',$id)->setField('is_read_shop',1);
        }

        $refund = Db::name('order_refund ors')->field('ors.*,rr.title')->where('order_id',$find['id'])->join('refund_reason rr','ors.reason_id = rr.id')->find();
        if (Request::instance()->isPost()) {

            if(!empty($param['assignor_id'])){
                if (count($param['assignor_id']) != count(array_unique($param['assignor_id']))) {
                    //fail('不能添加重复人员');
                }
                //$single_id = explode('-',$param['single_id'][0])[0];//门店服务分类 第一个类就代表所有相同的服务类了
               // $single_title = Db::name('single')->where('id',$single_id)->value('cat_name');//服务类型
               // $assignor_price = explode('-',$param['single_id'][0])[1];//门店金额
                for ($i = 0; $i < count($param['assignor_id']); $i++) {
                    $shop_assignor_records[$i]['order_id'] = $find['id'];
                    $shop_assignor_records[$i]['shop_id'] = $find['shop_id'];
                    $shop_assignor_records[$i]['assignor_id'] = $param['assignor_id'][$i];
                    $shop_assignor_records[$i]['single_id'] = $param['single_id'][$i];
                    $shop_assignor_records[$i]['assignor_price'] = $param['assignor_price'][$i];
                    $shop_assignor_records[$i]['assignor_content'] = $param['single_id'][$i];
                    $shop_assignor_records[$i]['create_time'] = date('Y-m-d H:i:s', time());
                    $shop_assignor_records[$i]['order_type'] = $find['order_type'];
                    $shop_assignor_records[$i]['is_agree'] = 2;
//                        Db::name('gift_assignor_records')->insert(
//                            ['assignor_id' => $param['assignor_id'][$i],
//                                'assignor_price' => $param['assignor_price'][$i],
//                                'order_id' => $find['id'],
//                                'order_type' => $find['order_type'],
//                                'assignor_content' => $param['assignor_content'][$i],
//                                'create_time' => date('Y-m-d H:i:s',time()),
//                                'shop_id' => $shop_id
//                            ]);
                }
                if (isset($param['status']) && ($param['status'] == 3)) {
                    $ret = Db::name('order')->where('id', $param['id'])->update(['status' => 3]);
                }

                Db::name('shop_assignor_records')->insertAll($shop_assignor_records);
                win('修改成功', Url::build('index'));

            }
            if (isset($param['status']) && ($param['status'] == 3)) {

                Db::name('order o')->where('id',$id)->where('status',2)->setField('status',3);
                win('修改成功', Url::build('index'));
            }
            if (isset($param['status']) && ($param['status'] >= 10)) {

                $fail_explain = Request::instance()->param('fail_explain','');
                if(strpos('5,7,8,9,10',(string)$level) === false){
                    fail('当前管理员无退款审核权限');
                }
                $refund_order_audit = Db::name('refund_order_audit')->where([
                    'order_id' => $find['id'],
                    'manage_id' => \session('manage_id'),
                ])->find();//当前管理员自己提交的数据
                if(!empty($refund_order_audit)) fail('已经申请过审核');
                if($level == 5){ //门店点击提交 店长-副经理-经理-财务-总经理
                    if(empty($refund_order_audit)){
                        (new RefundOrderAudit())->save([
                            'order_id' => $find['id'],
                            'manage_id' => \session('manage_id'),
                            'status' => $param['status'],
                            'is_read' => 1,
                            'level' => $level,
                            'shop_id' => \session('shop_id'),
                            'shop_title' => Db::name('shop')->where('id',\session('shop_id'))->value('shop_title'),
                            'fail_explain' => $fail_explain,
                            'create_time' => date('Y-m-d H:i:s',time())
                        ]);
                        Db::name('order')->where('id',$find['id'])->setField('is_read_5',1);
                        if($param['status'] == 12){
                            Db::name('order')->where('id',$find['id'])->setField('status',12);
                        }
                        win('门店申请退款审核',Url::build('index'));
                    }else{
                        fail('已经申请审核');
                    }
                }
                $refund_order_audit_5 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>5])->find();//门店是否提交审核数据
                $refund_order_audit_7 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>7])->find();//门店是否提交审核数据
                $refund_order_audit_8 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>8])->find();//门店是否提交审核数据
                $refund_order_audit_9 = Db::name('refund_order_audit')->where(['order_id' => $find['id'],'status' => 11, 'level' =>9])->find();//门店是否提交审核数据
                if($level == 7){ //副经理
                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');
                    $ret = (new RefundOrderAudit())->save([
                        'order_id' => $find['id'],
                        'manage_id' => \session('manage_id'),
                        'status' => $param['status'],
                        'is_read' => 1,
                        'level' => $level,
                        'shop_id' => 0,
                        'fail_explain' => $fail_explain,
                        'create_time' => date('Y-m-d H:i:s',time())
                    ]);
                    Db::name('order')->where('id',$find['id'])->setField('is_read_7',1);
                    if($param['status'] == 12){
                        Db::name('order')->where('id',$find['id'])->setField('status',12);
                    }
                    if($ret){
                        win('副经理申请退款审核',Url::build('index'));
                    }

                }
                if($level == 8){ //经理

                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');
                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');
                    $ret = (new RefundOrderAudit())->save([
                        'order_id' => $find['id'],
                        'manage_id' => \session('manage_id'),
                        'status' => $param['status'],
                        'is_read' => 1,
                        'level' => $level,
                        'shop_id' => 0,
                        'fail_explain' => $fail_explain,
                        'create_time' => date('Y-m-d H:i:s',time())
                    ]);
                    Db::name('order')->where('id',$find['id'])->setField('is_read_8',1);
                    if($param['status'] == 12){
                        Db::name('order')->where('id',$find['id'])->setField('status',12);
                    }
                    if($ret){
                        win('经理申请退款审核',Url::build('index'));
                    }
                }
                if($level == 9){ //财务

                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');
                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');
                    if(empty($refund_order_audit_8)) fail('经理没有通过退款审核');
                    $ret = (new RefundOrderAudit())->save([
                        'order_id' => $find['id'],
                        'manage_id' => \session('manage_id'),
                        'status' => $param['status'],
                        'is_read' => 1,
                        'level' => $level,
                        'shop_id' => 0,
                        'fail_explain' => $fail_explain,
                        'create_time' => date('Y-m-d H:i:s',time())
                    ]);
                    Db::name('order')->where('id',$find['id'])->setField('is_read_9',1);
                    if($param['status'] == 12){
                        Db::name('order')->where('id',$find['id'])->setField('status',12);
                    }
                    if($ret){
                        win('财务申请退款审核',Url::build('index'));
                    }
                }
                if($level == 10){ //总经理

                    if(empty($refund_order_audit_5)) fail('门店没有通过退款审核');
                    if(empty($refund_order_audit_7)) fail('副经理没有通过退款审核');
                    if(empty($refund_order_audit_8)) fail('经理没有通过退款审核');
                    if(empty($refund_order_audit_9)) fail('财务没有通过退款审核');
                    Db::name('order')->where('id',$find['id'])->setField('is_read_10',1);
                    if($param['status'] == 12){
                        Db::name('order')->where('id',$find['id'])->setField('status',12);
                    }
                    if($param['status'] == 11){
                        $find = Db::name('order')->find($id);
                        if ($find['pay_type'] == '2') {
                            $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '', $find['shop_order_number']);
                            if ($ret->code !== '10000') {
                                fail($ret->sub_msg);
                            }
                            //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);
                        } else if ($find['pay_type'] == '1') {
                            //halt('wx');
                            $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);
                            if ($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);
                        }
                        $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);
                        if ($ret !== false) {
                            (new Order())->refund($find['member_id'], $find['order_number']);
                            $refund_ret = Db::name('order_refund')->where('order_id', $id)->update(['fail_explain' => $param['fail_explain']]);
                            if ($refund_ret !== false) {
                                win('退货款处理完毕', Url::build('index'));
                            }
                        }
                        $ret = (new RefundOrderAudit())->save([
                            'order_id' => $find['id'],
                            'manage_id' => \session('manage_id'),
                            'status' => $param['status'],
                            'is_read' => 1,
                            'level' => $level,
                            'shop_id' => 0,
                            'fail_explain' => $fail_explain,
                            'create_time' => date('Y-m-d H:i:s',time())
                        ]);
                        if($ret){
                            win('总经理同意退款中',Url::build('index'));
                        }
                    }

                }





            } //退款逻辑end
//            if (isset($param['status']) && ($param['status'] == 11)) {
//                $find = Db::name('order')->find($id);
//                if ($find['pay_type'] == '2') {
//                    $ret = (new Alipay())->refund($find['order_number'], $refund['amount'], '',$find['shop_order_number']);
//                    if($ret->code !== '10000'){
//                        fail($ret->sub_msg);
//                    }
//                    //halt($data['order_number'] . '-' . $data['price'] . '-' . $data['explain']);
//                } else if ($find['pay_type'] == '1') {
//                    //halt('wx');
//                    $ret = (new Alipay())->wechat($find['order_number'], $find['amount'], $refund['amount']);
//                    if($ret['result_code'] == 'FAIL') fail($ret['err_code_des']);
//                }
//                $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);
//                if ($ret !== false) {
//                    $this->refund($find['member_id'],$find['order_number']);
//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);
//                    if($refund_ret !== false){
//                        win('退货款处理完毕', Url::build('index'));
//                    }
//                }
//            }
//
//            if (isset($param['status']) && ($param['status'] == 12)) {
//                $ret = Db::name('order')->where('id', $param['id'])->update(['status' => $param['status']]);
//                if ($ret !== false) {
//                    $refund_ret = Db::name('order_refund')->where('order_id',$id)->update(['fail_explain' =>$param['fail_explain']]);
//                    if($refund_ret !== false){
//                        win('退货款失败处理完毕', Url::build('index'));
//                    }
//                }
//            }
        }
        $info = Db::name('order o')
            ->join('member m', 'o.member_id = m.id','left')
            ->join('shop s', 'o.shop_id = s.id','left')
            ->field('o.*,m.username,s.shop_title,m.car_number,m.brand_number,m.id_code')
            ->where('o.id', $param['id'])
            ->find();
        if($info['status'] == 10 || $info['status'] == 11 || $info['status'] == 12){
            $info['refund'] = Db::name('order_refund')->where('order_id',$info['id'])->field('explain,amount price,type')->find();
            $info['refund_create_time'] = Db::name('order_refund')->where('order_id',$info['id'])->value('create_time');
        }
        $info['goods_list'] = Db::name('order_goods og')
            ->join('shop_goods sg', 'og.goods_id = sg.id')
            ->field('og.*,sg.thumb_img')
            ->where('order_id', $id)
            ->select();

        $refund_order_audit_records = (new RefundOrderAudit())
            ->alias('roa')
            ->join('manager m','roa.manage_id = m.id')
            ->field('roa.*,m.username')
            ->where('order_id',$find['id'])
            ->select();
        //审核会员数量 不管通过不通过;
        $count = (new RefundOrderAudit())->where('order_id',$find['id'])->where('status','11')->count();
        $refund_button = false; //这么判断 其他人员肯定是false
        if($level == 5){
            if($count == 0){
                $refund_button = true;
            }
        }
        if($level == 7){
            if($count == 1){
                $refund_button = true;
            }
        }
        if($level == 8){
            if($count == 2){
                $refund_button = true;
            }
        }
        if($level == 9){
            if($count == 3){
                $refund_button = true;
            }
        }
        if($level == 10){
            if($count == 4){
                $refund_button = true;
            }
        }
        $shop_assignor_records = Db::name('shop_assignor_records sar')
            ->join('shop','shop.id = sar.shop_id','left')
            ->join('shop_assignor sa','sar.assignor_id = sa.id','left')
            ->join('goods_type gt','gt.id = sar.single_id','left')
            ->where('order_id', $info['id'])
            ->field('shop.shop_title,gt.title,sa.*,sar.*')
            ->order('sar.id','desc')
            ->select()->toArray();

        if(!empty($shop_assignor_records)){
            foreach ($shop_assignor_records as $k => &$v){
                if($v['order_type'] == 2){
                    $v['single_name'] = $v['cat_name'];
                }
                if($v['order_type'] == 1){
                    $v['single_name'] = $v['title'];

                }
                //$v['assignor_content'] = Db::name('single')->where('id',$v['assignor_content'])->value('cat_name');
                $v['assignor_content'] = $v['title'];
            }
        }

        $shop_assignor = Db::name('shop_assignor')->where('shop_id',$find['shop_id'])->select();
        $service_people_price = Db::name('shop_service_price')
            ->alias('ssp')
            ->join('single s','ssp.single_id = s.id','left')
            ->where('shop_id',$find['shop_id'])
            ->where('s.parent_id','eq',0)
            ->where('auding_status',2)
            ->where('is_delete',1)
            ->field('s.id,s.cat_name,ssp.person_price')
            ->select();

        if(session('role_id') != 1){
            $info['username'] = hideStr($info['username'],1,6);
        }

        $service_people_price2 = Db::name('single')
            ->where('auding_status',2)
            ->where('is_delete',1)
            ->where('parent_id',1)
            ->select();
        $order_goods = Db::name('order_goods')
            ->alias('og')
            ->where('og.order_id',$info['id'])
            ->join('shop_goods sg','og.goods_id = sg.id')
            ->join('goods_type gt','gt.id = sg.goods_id')
            ->group('gt.id')
            ->field('gt.id,sum(number) number,og.title')
            ->select()->toArray();
        $go = '';

        foreach ($order_goods as $k1 => $v1){
            $go .= $v1['title'].' ';
        }
        // 必须$v1 否则重复定义 halt($shop_assignor_records);
        $renyuan['one_str'] = '';
        $renyuan['three_str'] = '';
        $renyuan['last_person_price'] = '';
        if(!empty($order_goods)){
            foreach ($order_goods as $k => &$v){
                $v['three_level_id'] = $v['id'];
                $v['three_level_name'] = Db::name('goods_type')->where('id',$v['id'])->value('title');
                $price = Db::name('goods_type')->where('id',$v['id'])->value('price');
                $two_id = Db::name('goods_type')->where('id',$v['id'])->value('parenTid');
                $v['two_level_id'] = Db::name('goods_type')->where('id',$v['id'])->value('parenTid');
                $v['two_level_name'] = Db::name('goods_type')->where('id',$two_id)->value('title');
                $v['one_level_id'] = Db::name('goods_type')->where('id',$two_id)->value('parenTid');
                $three_id = Db::name('goods_type')->where('id',$two_id)->value('parenTid');
                $v['one_level_name'] = Db::name('goods_type')->where('id',$three_id)->value('title');
                $v['person_price'] = $price*$v['number'];
                $renyuan['one_str'] .= $v['one_level_name'].' ';
                $renyuan['three_str'] .= $v['three_level_name'].' ';
                $renyuan['last_person_price'] += $price*$v['number'];
            }
        }
        $template = '';
        if(isset($param['table'])) $template = 'service_order_table';
        $info['cat_name'] = $go;
        $info['one_cat'] = '';
        $info['order_number'] = $info['shop_order_number'];


        return view($template, ['item' => $info,
            'order_goods' => json_encode($order_goods),
            'service_people_price' => $service_people_price,
            'shop_assignor' => $shop_assignor,
            'service_people_price2' => json_encode($service_people_price2),
            'refund' => $refund,'refund_order_audit_records' => $refund_order_audit_records,
            'refund_button' => $refund_button,
            'category' => json_encode(ClassifyAlias::dimension(Cache::get('goodsType'))),
            'shop_assignor_records' => $shop_assignor_records,
            'gift_assignor_records' => $shop_assignor_records,]);
    }

    public function ajaxfenlei($id, $order_id)
    {
        $order_goods = Db::name('order_goods')
            ->alias('og')
            ->where('og.order_id', $order_id)
            ->where('gt.id',$id)
            ->join('shop_goods sg', 'og.goods_id = sg.id')
            ->join('goods_type gt', 'gt.id = sg.goods_id')
            ->group('gt.id')
            ->field('gt.id,sum(number) number')
            ->select()->toArray();

        $renyuan['one_str'] = '';
        $renyuan['three_str'] = '';
        $renyuan['last_person_price'] = '';
        if (!empty($order_goods)) {
            foreach ($order_goods as $k => &$v) {
                $v['three_level_id'] = $v['id'];
                $v['three_level_name'] = Db::name('goods_type')->where('id', $v['id'])->value('title');
                $price = Db::name('goods_type')->where('id', $v['id'])->value('price');
                $two_id = Db::name('goods_type')->where('id', $v['id'])->value('parenTid');
                $v['two_level_id'] = Db::name('goods_type')->where('id', $v['id'])->value('parenTid');
                $v['two_level_name'] = Db::name('goods_type')->where('id', $two_id)->value('title');
                $v['one_level_id'] = Db::name('goods_type')->where('id', $two_id)->value('parenTid');
                $three_id = Db::name('goods_type')->where('id', $two_id)->value('parenTid');
                $v['one_level_name'] = Db::name('goods_type')->where('id', $three_id)->value('title');
                $v['person_price'] = $price * $v['number'];
                $renyuan['one_str'] .= $v['one_level_name'] . ' ';
                $renyuan['three_str'] .= $v['three_level_name'] . ' ';
                $renyuan['last_person_price'] += $price * $v['number'];
            }
        }
        $service_price = Db::name('goods_type')->where('id', $id)->value('price');
        return $service_price*$order_goods[0]['number'];
    }

    /**
     * 商品和服务 退款消息提醒
     */
    public function refund_news()
    {
        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
        if($level == 5) {
            $refund_order_5 =
                Db::name('order')
                    ->whereOr('order_type', 1)
                    ->whereOr('order_type', 2)
                    ->where('shop_id',session('shop_id'))
                    ->where('status',10)
                    ->where('is_read_5',0)
                    ->find();//商品和服务

            $refund_order_55 =
                Db::name('order')
                    ->where('shop_id',0)
                    ->where('order_type',3)
                    ->where('status',10)
                    ->where('is_read_5',0)
                    ->find();
            if($refund_order_5){
                return ['code' => 200, 'message' => '查询到5未读商品或者服务'.Db::getlastSql().$level];
            }
            if($refund_order_55){
                //return ['code' => 200, 'message' => '查询到5未读礼包'.Db::getlastSql().$level];
            }
        }
        if($level == 7) {
            $str =
                Db::name('order')
                    ->whereOr('order_type', 1)
                    ->whereOr('order_type', 2)
                    ->where('is_read_5',1)->where('is_read_7',0)->column('id');
            $str = implode(',',$str);
            $refund_order_5 = Db::name('order')
                ->whereOr('order_type', 1)
                ->whereOr('order_type', 2)
                ->where('is_read_5',1)->where('is_read_7',0)->find();
            if($refund_order_5){
                return ['code' => 200, 'message' => '商品和服务 退款查询到7未读'.Db::getlastSql().$level.'/'.$refund_order_5['id'].'/id_str='.$str];
            }
        }
        if($level == 8) {
            $refund_order_5 = Db::name('order')
                ->whereOr('order_type', 1)
                ->whereOr('order_type', 2)
                ->where('is_read_7',1)->where('is_read_8',0)->find();
            if($refund_order_5){
                return ['code' => 200, 'message' => '商品和服务 退款查询到8未读'.Db::getlastSql().$level];
            }
        }
        if($level == 9) {
            $refund_order_5 = Db::name('order')
                ->whereOr('order_type', 1)
                ->whereOr('order_type', 2)
                ->where('is_read_8',1)->where('is_read_9',0)->find();
            if($refund_order_5){
                return ['code' => 200, 'message' => '商品和服务 退款查询到9未读'.Db::getlastSql().$level];
            }
        }
        if($level == 10) {
            $refund_order_5 = Db::name('order')
                ->whereOr('order_type', 1)
                ->whereOr('order_type', 2)
                ->where('is_read_9',1)->where('is_read_10',0)->find();
            if($refund_order_5){
                return ['code' => 200, 'message' => '商品和服务 退款查询到10未读'.Db::getlastSql().$level];
            }
        }
        return ['code' => 0, 'message' => '非管理类'];

    }

    /**
     * @return array
     * 退款单数量
     */
    public function refund_service_count()
    {
        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
        if($level == 5) {
            $count = Db::name('order')->where('shop_id',session('shop_id'))->where('order_type',2)->where('status',10)->where('is_read_5',0)->count();
            return ['code' => 200,'message' => '订单消息','count' => $count];
        }
        if($level == 7){
            $count = Db::name('order')->where('order_type',2)->where('status',10)->where('is_read_5',1)->where('is_read_7',0)->count();
            return ['code' => 200,'message' => '订单消息','count' => $count];
        }
        if($level == 8){
            $count = Db::name('order')->where('order_type',2)->where('status',10)->where('is_read_7',1)->where('is_read_8',0)->count();
            return ['code' => 200,'message' => '订单消息','count' => $count];
        }
        if($level == 9){
            $count = Db::name('order')->where('order_type',2)->where('status',10)->where('is_read_8',1)->where('is_read_9',0)->count();
            return ['code' => 200,'message' => '订单消息','count' => $count];
        }
        if($level == 10){
            $count = Db::name('order')->where('order_type',2)->where('status',10)->where('is_read_9',1)->where('is_read_10',0)->count();
            return ['code' => 200,'message' => '订单消息','count' => $count];
        }

    }
    public function refund_goods_count()
    {
        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
        if($level == 5) {
            $count = Db::name('order')->where('shop_id',session('shop_id'))->where('order_type',1)->where('status',10)->where('is_read_5',0)->count();
            return ['code' => 200,'message' => '商品消息','count' => $count];
        }
        if($level == 7){
            $count = Db::name('order')->where('order_type',1)->where('status',10)->where('is_read_5',1)->where('is_read_7',0)->count();
            return ['code' => 200,'message' => '商品消息','count' => $count];
        }
        if($level == 8){
            $count = Db::name('order')->where('order_type',1)->where('status',10)->where('is_read_7',1)->where('is_read_8',0)->count();
            return ['code' => 200,'message' => '商品消息','count' => $count];
        }
        if($level == 9){
            $count = Db::name('order')->where('order_type',1)->where('status',10)->where('is_read_8',1)->where('is_read_9',0)->count();
            return ['code' => 200,'message' => '商品消息','count' => $count];
        }
        if($level == 10){
            $count = Db::name('order')->where('order_type',1)->where('status',10)->where('is_read_9',1)->where('is_read_10',0)->count();
            return ['code' => 200,'message' => '商品消息','count' => $count];
        }

    }

    public function refund_gift_count()
    {
        $level = \session('role_id');//当前登录的管理员界别 5 7 8 9 10
//        if($level == 5) {
//            $count2 =
//                Db::name('order')
//                    ->where('order_type',3)
//                    ->where('status',10)
//                    ->where('is_read_5',0)
//                    ->count();
//            if($count2){
//                return ['code' => 200, 'message' => '查询到5未读礼包'.Db::getlastSql().$level,'count' =>$count2];
//            }
//        }
        //礼包退款取消 门店判断
        if($level == 7){ //副店长看门店是否提交退款了;
            $count = Db::name('order')->where('order_type',3)->where('status',10)->where('is_read_7',0)->count();
            return ['code' => 200,'message' => '礼包消息','count' => $count,'level' => $level];
        }
        if($level == 8){
            $count = Db::name('order')->where('order_type',3)->where('status',10)->where('is_read_7',1)->where('is_read_8',0)->count();
            return ['code' => 200,'message' => '礼包消息','count' => $count,'level' => $level];
        }
        if($level == 9){
            $count = Db::name('order')->where('order_type',3)->where('status',10)->where('is_read_8',1)->where('is_read_9',0)->count();
            return ['code' => 200,'message' => '礼包消息','count' => $count,'level' => $level];
        }
        if($level == 10){
            $count = Db::name('order')->where('order_type',3)->where('status',10)->where('is_read_9',1)->where('is_read_10',0)->count();
            return ['code' => 200,'message' => '礼包消息','count' => $count,'level' => $level];
        }

    }

    /**
     * @param $member_id
     * @param $order_number
     * 订单退款
     */
    public function refund($member_id, $order_number)
    {
        writeLogOrder($member_id . '/' . $order_number);
        $manage_id = Db::name('share_member')->where('member_id', $member_id)->value('manage_id');//获得直属业务员
        $salesman_array = parentPath($manage_id, 'salesman', true);//当前业务员上级
        $salesman_list = array_reverse($salesman_array);
        // $salesman_list = array_slice($salesman_array,0,3);

        if ($salesman_list) {
            $pre = 0;
            foreach ($salesman_list as $k => $v) {
                if ($k > 2) break;
                Db::name('member_distribution_records')->where('member_id', $member_id)->where('manage_id', $v['manage_id'])->where('order_number', $order_number)->delete();
            }
        }
    }

    public function news()
    {
        $time = date('Y-m-d H:i:s',time() - 36000);//五分钟
        $times = time()- 36000;//五分钟
       // $date = date('Y-m-d H:i:s', $time);
        $ret = Db::name('order')
            ->whereOr('order_type', 1)
            ->whereOr('order_type', 2)
            ->where('is_read',0)
            //->whereTime('create_time','>',$times)
            ->fetchSql(false)->find();
        //whereTime('create_time', '>=', $date)->
        if ($ret) {
            return ['code' => 200, 'message' => '查询到最新订单平台'.Db::getlastSql()];
        }
        return ['code' => 0, 'message' => '没有'];

    }

    public function service_count()
    {
        $count = Db::name('order')->where('order_type',2)->where('is_read',0)->count();
        return ['code' => 200,'message' => '订单消息','count' => $count];
    }
    public function goods_count()
    {
        $count = Db::name('order')->where('order_type',1)->where('is_read',0)->count();
        return ['code' => 200,'message' => '订单消息','count' => $count];
    }
    public function gift_count()
    {
        $count = Db::name('order')->where('order_type',3)->where('is_read',0)->count();
        return ['code' => 200,'message' => '订单消息gift_count','count' => $count];
    }


    /**
     * @return array商家订单提醒 只用于商品和服务订单
     */
    public function news_shop()
    {
        $time = time() - 600;//五分钟
        $date = date('Y-m-d H:i:s', $time);
        $auding_status = Session::get('auding_status');
        if($auding_status == 1){
            $where['shop_id'] = ['eq',\session('shop_id')];
        }
        $ret = Db::name('order')
            ->whereOr('order_type', 1)
            ->whereOr('order_type', 2)
            ->where('is_read_shop',0)
            ->where($where)
//            ->where('create_time','lt',$time)
            ->fetchSql(false)->find();
        //whereTime('create_time', '>=', $date)->
        if ($ret) {
            return ['code' => 200, 'message' => '查询到最新订单门店 提示音order_type 1 2 shop_id='.\session('shop_id').'order_id='.$ret['id']];
        }
        return ['code' => 0, 'message' => '没有'];

    }

    /**
     * 门店礼包消息提醒
     */
    public function news_gift()
    {
        //如果默认是全部未读
        $order = Db::name('order')->where('order_type', 3)->select()->toArray();
        $ret = false;//全部是已读消息 如果有一个未读就返回其他状态
        if (!empty($order)) {
            foreach ($order as $k => $v) {
                //按照管理员ID来 没有门店ID //发现一个空的就是没有查询大 就是未读
                $gift_news = Db::name('gift_news')->where('order_id', $v['id'])->where('manage_id',session('manage_id'))->find();
                //非空代表已读 空代表 未读过
                if(empty($gift_news)){ //包含有未读消息
                    $ret = true;
                }
            }
        }
        return $ret;
    }

    /**
     * @return int
     * 礼包消息提醒
     */
    public function news_gift_count()
    {
        $order = Db::name('order')->where('order_type', 3)->select()->toArray();
        $ret = 0;//默认没有未读消息0
        if (!empty($order)) {
            foreach ($order as $k => $v) {
                //按照管理员ID来 没有门店ID
                $gift_news = Db::name('gift_news')->where('order_id', $v['id'])->where('manage_id',session('manage_id'))->find();
                if(empty($gift_news)){ //包含有未读消息
                    $ret++;
                }
            }
        }
        return $ret;
    }

    /**
     * @return array
     * 门店未读订单
     */
    public function service_count_shop()
    {
        $where = [];
        $auding_status = Session::get('auding_status');
        if($auding_status == 1){
            $where['shop_id'] = ['eq',\session('shop_id')];
        }
        $count = Db::name('order')->where($where)->where('order_type',2)->where('is_read_shop',0)->count();
        return ['code' => 200,'message' => '订单消息','count' => $count];
    }
    public function goods_count_shop()
    {
        $where = [];
        $auding_status = Session::get('auding_status');
        if($auding_status == 1){
            $where['shop_id'] = ['eq',\session('shop_id')];
        }
        $count = Db::name('order')->where($where)->where('order_type',1)->where('is_read_shop',0)->count();
        return ['code' => 200,'message' => '订单消息','count' => $count];
    }
    public function gift_count_shop()
    {
        $where = [];
        $auding_status = Session::get('auding_status');
        if($auding_status == 1){
            $where['shop_id'] = ['eq',\session('shop_id')];
        }
        $count = Db::name('order')->where('order_type',3)->where('is_read_shop',0)->count();

        return ['code' => 200,'message' => '订单消息','count' => $count];
    }

    //礼包退款提示音
    public function refund_gift_news()
    {
        $level = \session('role_id');//当前登录的管理员界别 门店管理员 5 7 8 9 10
//        if($level == 5) {
//            $refund_order_5 =
//                Db::name('order')
//                    ->whereOr('order_type', 1)
//                    ->whereOr('order_type', 2)
//                    ->where('shop_id',session('shop_id'))
//                    ->where('status',10)
//                    ->where('is_read_5',0)
//                    ->find();//商品和服务
//            $refund_order_55 =
//                Db::name('order')
//                    ->where('shop_id',0)
//                    ->where('order_type',3)
//                    ->where('status',10)
//                    ->where('is_read_5',0)
//                    ->find();
//            if($refund_order_5){
//                return ['code' => 200, 'message' => '礼包退款提示音,查询到5未读商品或者服务'.Db::getlastSql().$level];
//            }
//            if($refund_order_55){
//                //return ['code' => 200, 'message' => '查询到5未读礼包'.Db::getlastSql().$level];
//            }
//        }
        if($level == 7) {
            $refund_order_5 = Db::name('order')
                ->where('order_type', 3)
                ->where('status',10)
                ->where('is_read_7',0)->find();
            $str = Db::name('order')
                ->where('order_type', 3)
                ->where('status',10)
                ->where('is_read_7',0)->column('id');
            if($refund_order_5){
                return ['code' => 200, 'message' => '礼包副经理查询到7未读'.Db::getlastSql().'@级别'.$level.'/总数'.count($str).'--'.implode(',',$str)];
            }
        }
        if($level == 8) {
            $refund_order_5 = Db::name('order')
                ->where('status',10)
                ->where('order_type', 3)
                ->where('is_read_7',1)->where('is_read_8',0)->find();
            if($refund_order_5){
                return ['code' => 200, 'message' => '礼包经理查询到8未读'.Db::getlastSql().$level.'/'.$refund_order_5['id']];
            }
        }
        if($level == 9) {
            $refund_order_5 = Db::name('order')
                ->where('status',10)
                ->where('order_type', 3)
                ->where('is_read_8',1)->where('is_read_9',0)->find();
            if($refund_order_5){
                return ['code' => 200, 'message' => '礼包财务查询到9未读'.Db::getlastSql().$level.'/'.$refund_order_5['id']];
            }
        }
        if($level == 10) {
            $refund_order_5 = Db::name('order')
                ->where('status',10)
                ->where('order_type', 9)
                ->where('is_read_9',1)->where('is_read_10',0)->find();
            if($refund_order_5){
                return ['code' => 200, 'message' => '礼包总经理查询到10未读'.Db::getlastSql().$level.'/'.$refund_order_5['id']];
            }
        }
        return ['code' => 0, 'message' => '非管理类'];

    }

    public function abc(){
        $order = Db::name('order')->where('order_number','201902011509015495810829')->fetchSql(true)->find();//订单所有数据
        halt($order);
    }

}